package org.kernely.ui.settings;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class KernelySettingsView extends GridLayout implements IKernelyView {
	
	private static final long serialVersionUID = 1056106551489049821L;
	private TextField oldPasswordField;
	private TextField newPasswordField;
	private TextField confirmPasswordField;
	private Button savePasswordButton;
	private Label title;
	private Label passwordsTitle;
	private Panel passwordsPanel;
	
	/**
	 * KernelySettingsView constructor.
	 */
	public KernelySettingsView(){
		title = new Label("Settings");
		title.setStyleName(Reindeer.LABEL_H1);
		title.setSizeUndefined(); // Usefull to center the label.

		// Password components
		
		passwordsPanel = new Panel();
		passwordsPanel.setSizeUndefined();
		passwordsTitle = new Label("Password");
		passwordsTitle.setStyleName(Reindeer.LABEL_H2);
		
		oldPasswordField = new TextField("Password");
		newPasswordField = new TextField("New password");
		confirmPasswordField = new TextField("Confirm password");
		oldPasswordField.setSecret(true);
		newPasswordField.setSecret(true);
		confirmPasswordField.setSecret(true);
		oldPasswordField.setWidth(200,UNITS_PIXELS);
		newPasswordField.setWidth(200,UNITS_PIXELS);
		confirmPasswordField.setWidth(200,UNITS_PIXELS);
		savePasswordButton = new Button("Save");		
		oldPasswordField.setRequired(true);
		newPasswordField.setRequired(true);
		confirmPasswordField.setRequired(true);
		oldPasswordField.setRequiredError("You must enter your old password.");
		newPasswordField.setRequiredError("You must enter a new password.");
		confirmPasswordField.setRequiredError("You must fill the \"Confirm password\" field.");
		
		VerticalLayout passwordsLayout = new VerticalLayout();
		passwordsPanel.setContent(passwordsLayout);
		passwordsPanel.addComponent(passwordsTitle);
		passwordsPanel.addComponent(oldPasswordField);
		passwordsPanel.addComponent(newPasswordField);
		passwordsPanel.addComponent(confirmPasswordField);
		passwordsPanel.addComponent(savePasswordButton);
		passwordsLayout.setComponentAlignment(savePasswordButton,Alignment.MIDDLE_RIGHT);
		passwordsLayout.setMargin(true,true,true,true);
		passwordsLayout.setSpacing(true);
		
		this.setColumns(4);
		this.setRows(2);
		this.addComponent(title,0,0,3,0);
		this.setComponentAlignment(title,Alignment.MIDDLE_CENTER);
		this.addComponent(passwordsPanel,0,1);
		this.setSizeFull();
	}
	
	
	
	
	/**
	 * Gets the oldPasswordField
	 * @return the oldPasswordField
	 */
	public final TextField getOldPasswordField() {
		return oldPasswordField;
	}




	/**
	 * Gets the newPasswordField
	 * @return the newPasswordField
	 */
	public final TextField getNewPasswordField() {
		return newPasswordField;
	}




	/**
	 * Gets the confirmPasswordField
	 * @return the confirmPasswordField
	 */
	public final TextField getConfirmPasswordField() {
		return confirmPasswordField;
	}




	/**
	 * Gets the savePasswordButton
	 * @return the savePasswordButton
	 */
	public final Button getSavePasswordButton() {
		return savePasswordButton;
	}




	/**
	 * This method return the component. (Useless because we use the MVP pattern, see the wiki).
	 */
	@Override
	public final Component getComponent() {
		return this;
	}

}
